package gemini.castor.ui.client.page.widget.billingbox;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.content.account.myorder.form.MyOrderFormModel;
import gemini.castor.ui.client.page.content.account.myorder.form.MyOrderProductDetail;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;

public class BillingWidget extends Composite {

	@UiTemplate("BillingWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, BillingWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public BillingWidget() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}
	
	
	public void loadData(MyOrderFormModel model){
		htmPurchaseBy.setHTML(model.getPurchaseName());
		htmId.setHTML(model.getDistributorCode());
		htmAddress.setHTML(model.getBillingAdd());
		htmAmountBeforeVAT.setHTML(sumAmountBeforeVAT(model)+"");
		htmAmountVAT.setHTML((sumAmountBeforeVAT(model)*10/100)+"");
		htmTotalDue.setHTML((sumAmountBeforeVAT(model)*110/100)+"");
		htmInWords.setHTML("VND");//TODO: call function read money 
		htmPaymentMethod.setHTML(model.getPaymentBy());
		htmVolumePoint.setHTML(model.getPoint());
		htmProductTotal.setHTML(model.getProductList().size()+"");
		htmDateOfVAT.setHTML(GuiUtils.format(new Date()));
		htmDateOfOrder.setHTML(model.getOrderDate());
		htmCodeOfOrder.setHTML(model.getCode());
		htmMonthOfOrder.setHTML(model.getOrderMonth());
		
		initOrderTable();
		flbProductDetail.removeAllRows();
		initHeader();
		RowFormatter rowFmt = flbProductDetail.getRowFormatter();

		int i = 1;
		for (MyOrderProductDetail productDetail : model.getProductList()) {
			if (i % 2 == 0) {
				rowFmt.setStyleName(i, "rowEven");
			}
			flbProductDetail.setHTML(i, 0, productDetail.getProduct().getCode());
			flbProductDetail.setHTML(i, 1,  productDetail.getProduct().getName());
			flbProductDetail.setHTML(i, 2,  productDetail.getQuantity()+"");
			flbProductDetail.setHTML(i, 3, (productDetail.getProduct().getPrices().get(0).getValue() * productDetail.getQuantity())+"");
			flbProductDetail.setHTML(i, 4, (productDetail.getProduct().getVolumePoint() * productDetail.getQuantity())+"");
			flbProductDetail.setHTML(i, 5, getYourPrice(productDetail,model.getDiscountScale())+"");
			i++;
		}
	}
	
	private void initOrderTable() {
		ColumnFormatter colFmt = flbProductDetail.getColumnFormatter();

		colFmt.setWidth(0, "10%");
		colFmt.setWidth(1, "50%");
		colFmt.setWidth(2, "10%");
		colFmt.setWidth(3, "10%");
		colFmt.setWidth(4, "10%");
		colFmt.setWidth(5, "10%");
	}

	private void initHeader(){
		flbProductDetail.setHTML(0, 0, HtmlUtils.bold(constants.htmCode()));
		flbProductDetail.setHTML(0, 1, HtmlUtils.bold(constants.htmName()));
		flbProductDetail.setHTML(0, 2, HtmlUtils.bold(constants.htmQty()));
		flbProductDetail.setHTML(0, 3, HtmlUtils.bold(constants.htmRPrice()));
		flbProductDetail.setHTML(0, 4, HtmlUtils.bold(constants.htmPoint()));
		flbProductDetail.setHTML(0, 5, HtmlUtils.bold(constants.htmYourPrice()));
		flbProductDetail.getRowFormatter().setStyleName(0, "tableHeader");
		flbProductDetail.setStyleName("ftbSummary");
	}
	
	private int getYourPrice(MyOrderProductDetail productDetail,String discountScale){
		int resutl = (int) (productDetail.getProduct().getPrices().get(0).getValue() * productDetail.getQuantity() * Integer.parseInt(discountScale));
		return resutl;
	}
	
	private int sumAmountBeforeVAT(MyOrderFormModel model){
		int result =0;
		for (MyOrderProductDetail productDetail : model.getProductList()) {
			result += getYourPrice(productDetail,model.getDiscountScale());
		}
		return result;
	}

	@UiField
	HTML htmPurchaseBy;
	
	@UiField
	HTML htmId;
	
	@UiField
	HTML htmAddress;
	
	@UiField
	HTML htmAmountBeforeVAT;
	
	@UiField
	HTML htmAmountVAT;
	
	@UiField
	HTML htmTotalDue;
	
	@UiField
	HTML htmDateOfVAT;
	
	@UiField
	HTML htmDateOfOrder;
	
	@UiField
	HTML htmCodeOfOrder;
	
	@UiField
	HTML htmMonthOfOrder;
	
	@UiField
	HTML htmInWords;
	
	@UiField
	HTML htmPaymentMethod;
	
	@UiField
	HTML htmVolumePoint;
	
	@UiField
	HTML htmProductTotal;
	
	@UiField
	FlexTable flbProductDetail;
	
	
	
	
}
